package com.atguigu.gmall.product.controller.admin;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrInfoVO;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * 商品属性信息表的controller类
 */
@RequestMapping("/admin/product")
@RestController
public class BaseAttrInfoController {

    @Autowired
    private BaseAttrInfoService baseAttrInfoService;

    /**
     * 根据三级分类id查询平台属性数据
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(@PathVariable("category1Id") Long category1Id,
                                  @PathVariable("category2Id") Long category2Id,
                                  @PathVariable("category3Id") Long category3Id){
        List<BaseAttrInfoVO> baseAttrInfoList = baseAttrInfoService.listByCategory3Id(category3Id);

        return Result.ok(baseAttrInfoList);
    }

    /**
     * 条件分页查询
     *
     * @param baseAttrInfo
     * @param pageNum
     * @param size
     * @return
     */
    @GetMapping("/search/{pageNum}/{size}")
    public Result search(@RequestBody BaseAttrInfo baseAttrInfo,
                         @PathVariable("pageNum") Integer pageNum,
                         @PathVariable("size") Integer size) {
        IPage page = baseAttrInfoService.search(baseAttrInfo, pageNum, size);
        return Result.ok(page);
    }

    /**
     * 条件查询
     *
     * @param baseAttrInfo
     * @return
     */
    @GetMapping("/search")
    public Result search(@RequestBody BaseAttrInfo baseAttrInfo) {
        List<BaseAttrInfo> baseAttrInfos = baseAttrInfoService.search(baseAttrInfo);
        return Result.ok(baseAttrInfos);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param size
     * @return
     */
    @GetMapping("/page/{pageNum}/{size}")
    public Result page(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("size") Integer size) {
        IPage<BaseAttrInfo> page = baseAttrInfoService.page(pageNum, size);
        return Result.ok(page);
    }

    /**
     * 根据主键id删除
     *
     * @return
     */
    @DeleteMapping("removeById/{id}")
    public Result removeById(@PathVariable("id") Long id) {
        baseAttrInfoService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据主键id修改
     *
     * @param baseAttrInfo
     * @return
     */
    @PutMapping("/editById")
    public Result editById(@RequestBody BaseAttrInfo baseAttrInfo) {
        baseAttrInfoService.editById(baseAttrInfo);
        return Result.ok();
    }

    /**
     * 新增数据
     *
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result save(@RequestBody BaseAttrInfo baseAttrInfo) {
        baseAttrInfoService.save(baseAttrInfo);
        return Result.ok();
    }

    /**
     * 主键查询
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Long id) {
        return Result.ok(baseAttrInfoService.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        return Result.ok(baseAttrInfoService.list());
    }

}
